﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using T2V.SharePoint.Common.Utilities;

namespace Pesico.Forms.ControlTemplates.Pesico.Forms
{
    public partial class ContractTemplateControl : UserControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            Common.Helpers.HiddenRibbon(this.Page, "Ribbon.ListForm.Edit.Actions");

            contractWizard.FinishButtonClick += new WizardNavigationEventHandler(contractWizard_FinishButtonClick);
            contractWizard.NextButtonClick += new WizardNavigationEventHandler(contractWizard_NextButtonClick);
            contractWizard.CancelButtonClick += new EventHandler(contractWizard_CancelButtonClick);
        }

        void contractWizard_CancelButtonClick(object sender, EventArgs e)
        {
            //Close popup
            this.Page.Response.Clear();
            this.Page.Response.Write(
            string.Format(System.Globalization.CultureInfo.InvariantCulture, @"<script type='text/javascript'> window.frameElement.commonModalDialogClose(1, '{0}');</script>", ""));
            this.Page.Response.End();
        }

        void contractWizard_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (!this.Page.IsValid)
                return;
            //Save item to list
            SaveItem();
            //Close popup
            this.Page.Response.Clear();
            this.Page.Response.Write(
            string.Format(System.Globalization.CultureInfo.InvariantCulture, @"<script type='text/javascript'> window.frameElement.commonModalDialogClose(1, '{0}');</script>", ""));
            this.Page.Response.End();
        }

        private void SaveItem()
        {
            SPListItem item = SPContext.Current.ListItem;
            item["Title"] = txtTitle.Text;

            item["CompanyA"] = txtCompanyA.Text;
            item["AddressA"] = txtAddressA.Text;
            item["AccountA"] = txtAccountA.Text;
            item["PhoneNumberA"] = txtPhoneNumberA.Text;
            item["FaxA"] = txtFaxA.Text;
            item["RepresentativeA"] = txtRepresentativeA.Text;

            item["CompanyB"] = txtCompanyB.Text;
            item["AddressB"] = txtAddressB.Text;
            item["AccountB"] = txtAccountB.Text;
            item["PhoneNumberB"] = txtPhoneNumberB.Text;
            item["FaxB"] = txtFaxB.Text;
            item["RepresentativeB"] = txtRepresentativeB.Text;


            item["ContractAmount"] = txtContractAmount.Text;
            item["ContractDate"] = txtContractDate.Text;

            SaveButton.SaveItem(SPContext.Current, false, string.Empty);
        }

        void contractWizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            // Collect the input data if going to the last page
            if (contractWizard.WizardSteps[e.NextStepIndex].StepType == WizardStepType.Finish)
            {
                //Caculate something
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            try
            {
                if (SPContext.Current.FormContext.FormMode == SPControlMode.New)
                    return;

                SPListItem item = SPContext.Current.ListItem;

                txtTitle.Text = item["Title"] != null ? item["Title"].ToString() : string.Empty;

                txtCompanyA.Text = item["CompanyA"] != null ? item["CompanyA"].ToString() : string.Empty;
                txtAddressA.Text = item["AddressA"] != null ? item["AddressA"].ToString() : string.Empty;
                txtAccountA.Text = item["AccountA"] != null ? item["AccountA"].ToString() : string.Empty;
                txtPhoneNumberA.Text = item["PhoneNumberA"] != null ? item["PhoneNumberA"].ToString() : string.Empty;
                txtFaxA.Text = item["FaxA"] != null ? item["FaxA"].ToString() : string.Empty;
                txtRepresentativeA.Text = item["RepresentativeA"] != null ? item["RepresentativeA"].ToString() : string.Empty;

                txtCompanyB.Text = item["CompanyB"] != null ? item["CompanyB"].ToString() : string.Empty;
                txtAddressB.Text = item["AddressB"] != null ? item["AddressB"].ToString() : string.Empty;
                txtAccountB.Text = item["AccountB"] != null ? item["AccountB"].ToString() : string.Empty;
                txtPhoneNumberB.Text = item["PhoneNumberB"] != null ? item["PhoneNumberB"].ToString() : string.Empty;
                txtFaxB.Text = item["FaxB"] != null ? item["FaxB"].ToString() : string.Empty;
                txtRepresentativeB.Text = item["RepresentativeB"] != null ? item["RepresentativeB"].ToString() : string.Empty;

                txtContractAmount.Text = item["ContractAmount"] != null ? item["ContractAmount"].ToString() : string.Empty;
                txtContractDate.Text = item["ContractDate"] != null ? item["ContractDate"].ToString() : string.Empty;

                if (SPContext.Current.FormContext.FormMode == SPControlMode.Display)
                {
                    txtTitle.Enabled = false;

                    txtCompanyA.Enabled = false;
                    txtAddressA.Enabled = false;
                    txtAccountA.Enabled = false;
                    txtPhoneNumberA.Enabled = false;
                    txtFaxA.Enabled = false;
                    txtRepresentativeA.Enabled = false;

                    txtCompanyB.Enabled = false;
                    txtAddressB.Enabled = false;
                    txtAccountB.Enabled = false;
                    txtPhoneNumberB.Enabled = false;
                    txtFaxB.Enabled = false;
                    txtRepresentativeB.Enabled = false;

                    txtContractAmount.Enabled = false;
                    txtContractDate.Enabled = false;

                    WizardStep3.Wizard.FinishCompleteButtonStyle.CssClass = "disabled";
                }
            }
            catch (Exception ex)
            {
                Utility.LogError(ex.Message, "Pesico.Forms.ControlTemplates.Pesico.Forms.ContractTemplateControl LoadData");
            }
        }

    }
}
